var setting = {
	data : {
		simpleData : {
			enable : true,
			idKey : "id",
			pIdKey : "parentId",
			rootPId : -1
		},
		key : {
			url : "nourl"
		}
	}
};
var setting2 = {
		data : {
			simpleData : {
				enable : true,
				idKey : "id",
				pIdKey : "parentId",
				rootPId : -1
			},
			key : {
				name: "emotionWord",
				url : "nourl",
			}
		}
	};
var ztree;
var ztree2;

var Menu = {
	id : "menuTable",
	table : null,
	layerIndex : -1
};

/**
 * 初始化表格的列
 */
Menu.initColumn = function() {
	var columns = [ 
		{
			field : 'selectItem',
			radio : true
		}, {
			title : '情感',
			field : 'emotionWord',
			align : 'center',
			valign : 'middle',
			sortable : true,
			width : '200px'
		}, {
			title : '维度',
			field : 'dimensionName',
			align : 'center',
			valign : 'middle',
			sortable : true,
			width : '200px'
		}, {
			title : '极性值',
			field : 'worth',
			align : 'center',
			valign : 'middle',
			sortable : true,
			width : '200px'
		}, {
			title : '故障',
			field : 'isfault',
			align : 'center',
			valign : 'middle',
			sortable : true,
			width : '200px'
		}
	]
	return columns;
};

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		q:{
			emotionWord: '',
			dimensionId: '',
			dimensionName: '',
			worth: '',
			isfault: '',
        },
//		dimension : {
//			parentId : 0,
//		},
		emotion: {parentId : 0}
	},
	created : function() {
//		this.getDimData(0);
	},
	mounted : function() {
//		this.treeGrid(this.selectId);
	},
	methods: {
		treeGrid : function() {
			var colunms = Menu.initColumn();
			var table = new TreeTable(Menu.id, baseURL
					+ "emotion/list?emotionWord="+this.q.emotionWord
					+"&dimensionId="+this.q.dimensionId
					+"&worth="+this.q.worth
					+"&isfault="+this.q.isfault,
					colunms);
			table.setExpandColumn(0);
			table.setIdField("id");
			table.setCodeField("id");
			table.setParentCodeField("parentId");
			table.setExpandAll(true);
			table.init();
			Menu.table = table;
		},
		clearTreeGrid:function(){
			$("div").removeClass("fixed-table-toolbar");
			$("div").removeClass("fixed-table-container");
			vm.treeGrid();
		},
//		getDimData : function(parentId) {
//			//加载菜单树
//			$.get(baseURL + "carmodel/listfilter/" + parentId, function(r) {
//				vm.dim1 = r
//			})
//		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.emotion = {};
			vm.emotion = {
				isfault : 0,
				dimensionId : 0,
				parentId : 0
			};
			$("#emoFilter").val("");
			$(".editHide").removeAttr("disabled");
			vm.getMenu();
			vm.getMenuMain();
		},
		update: function (event) {
			var id = vm.getId();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

			$(".editHide").attr("disabled","disabled");
            vm.getInfo(id)
		},
		downexcel:function(){
			location.href = baseURL + "emotion/export?emotionWord="+vm.q.emotionWord
					+"&dimensionId="+vm.q.dimensionId
					+"&worth="+vm.q.worth
					+"&isfault="+vm.q.isfault
					+"&token=" + token;
		},
		saveOrUpdate: function (event) {
			if (vm.validator()) {
				return;
			}
			var url = vm.emotion.id == null ? "emotion/save" : "emotion/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.emotion),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = vm.getId();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "emotion/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
//								$("#jqGrid").trigger("reloadGrid");
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "emotion/info/"+id, function(r){
                vm.emotion = r.emotion;
				vm.getMenu();
				vm.getMenuMain();
            });
		},
		getId : function() {
			var selected = $('#menuTable').bootstrapTreeTable('getSelections');
			if (selected.length == 0) {
				alert("请选择一条记录");
				return null;
			} else {
				return selected[0].id;
			}
		},
		getMenu : function() {
			//加载菜单树
			$.get(baseURL + "dimension/list/null/null/0", function(r) {
				//添加顶级菜单
				r.unshift({
					id : -1,
					name : "通用维度",
					parentId : 0,
				});
				r.push({
					id : 0,
					name : "主维度",
					open : true
				});
				ztree = $.fn.zTree.init($("#menuTree"), setting, r);
				var node = ztree.getNodeByParam("id", vm.emotion.dimensionId);
				ztree.selectNode(node);
				vm.emotion.dimensionName = node.name;
			})
		},
		getMenuMain : function() {
			var emoFilter='null';
			if ($("#emoFilter").val()) {
				emoFilter=$("#emoFilter").val();
			}
			//加载菜单树
			$.get(baseURL + "emotion/list2/"+emoFilter, function(r) {
				//添加顶级菜单
				if (!$("#emoFilter").val()) {
					r.unshift({
						id : 0,
						emotionWord : "主维度",
						open : true,
						parentId : 0
					});
				}
				ztree2 = $.fn.zTree.init($("#menuTree2"), setting2, r);
				var node = ztree2.getNodeByParam("id", vm.emotion.parentId);
				ztree2.selectNode(node);
				vm.emotion.parentName = node.emotionWord;
			})
		},
		menuTree : function() {
			layer.open({
				type : 1,
				offset : '50px',
				skin : 'layui-layer-molv',
				title : "选择维度",
				area : [ '300px', '450px' ],
				shade : 0,
				shadeClose : false,
				content : jQuery("#menuLayer"),
				btn : [ '确定', '取消' ],
				btn1 : function(index) {
					var node = ztree.getSelectedNodes();
					//选择
					vm.emotion.dimensionId = node[0].id;
					vm.emotion.dimensionName = node[0].name;
					layer.close(index);
				}
			});
		},
		menuTree2 : function() {
			vm.getMenu();
			layer.open({
				type : 1,
				offset : '50px',
				skin : 'layui-layer-molv',
				title : "选择维度",
				area : [ '300px', '450px' ],
				shade : 0,
				shadeClose : false,
				content : jQuery("#menuLayer"),
				btn : [ '确定', '取消' ],
				btn1 : function(index) {
					var node = ztree.getSelectedNodes();
					//选择
					vm.q.dimensionId = node[0].id;
					vm.q.dimensionName = node[0].name;
					layer.close(index);
				}
			});
		},
		menuTreeMain : function() {
			vm.getMenuMain();
			layer.open({
				type : 1,
				offset : '50px',
				skin : 'layui-layer-molv',
				title : "选择别名",
				area : [ '300px', '450px' ],
				shade : 0,
				shadeClose : false,
				content : jQuery("#menuLayer2"),
				btn : [ '确定', '取消' ],
				btn1 : function(index) {
					var node = ztree2.getSelectedNodes();
					//选择
					vm.emotion.parentId = node[0].id;
					vm.emotion.parentName = node[0].emotionWord;
					layer.close(index);
				}
			});
		},
		validator : function() {
//			if (vm.emotion.dimensionId==0) {
//				alert("维度不能为空");
//				return true;
//			}
			if(!vm.emotion.worth || isNaN(vm.emotion.worth) || vm.emotion.worth>0.8|| vm.emotion.worth<-0.8){
            	alert('极性值为范围-0.8~0.8的数字！');
                return true;
            }
		},
		reload : function(event) {
			vm.showList = true;
			Menu.table.setUrl=baseURL + + "emotion/list?emotionWord="+this.q.emotionWord
			+"&dimensionId="+this.q.dimensionId
			+"&worth="+this.q.worth
			+"&isfault="+this.q.isfault;
			Menu.table.refresh();
		}
	}
});